<?php

namespace app\admin\controller;

use app\common\model\Qiniu;
use app\common\model\Video as VideoModel;
use app\common\model\Config;
use app\model\VideoCateModel;
use app\service\VideoPayService;
use app\service\VideoService;

/**
 * 课程管理
 * Class Video
 * @package app\admin\controller
 */
class Video extends Base
{
    public $videoModel;
    public $videoCateModel;

    public function __construct()
    {
        parent::__construct();
        $this->videoModel = new VideoModel;
        $this->videoCateModel = new VideoCateModel();
    }

    /**
     * 课程列表
     * @return \think\response\Json
     */
    public function video_list()
    {
        if (request()->isPost()) {
            $data = input('');
            $where = array();
            $page = max(input('page'), 1);
            $size = max(input('size'), 20);
            if ($data['title']) {
                $where['a.title'] = [
                    'like', "%{$data['title']}%"
                ];
            }
            if($data['cate_id']){
                $where['a.cate_id'] = $data['cate_id'];
            }
            if($data['type']){
                $where['a.type'] = $data['type'];
            }
            $videoList = VideoService::S()->queryPage($where, $page, $size, 'a.*,b.name');
            $total = VideoService::S()->queryCount($where);
            if (!$videoList) {
                return returnJson(200, '');
            }
            foreach ($videoList as $k => $v) {
                $videoList[$k]['picture_src'] = tomedia($v['picture']);
                $videoList[$k]['video_src'] = tomedia($v['video']);
            }
            return returnJson(200, '', [
                'list' => $videoList,
                'size' => $size,
                'page' => $page,
                'total' => $total
            ]);
        }
    }

    /**
     * 课程添加/编辑
     * @return \think\response\Json
     */
    public function edit()
    {
        if (request()->isPost()) {
            $input = input();
            if (!$input['title']) {
                return returnJson(202, '请填写完整课程标题');
            }
            if (!$input['picture']) {
                return returnJson(202, '请上传主图');
            }
//            if (!$input['video']) {
//                return returnJson(202, '请上传视频');
//            }
            if (!$input['content']) {
                return returnJson(202, '请填写详情');
            }
            $data = array(
                'title' => $input['title'],
                'desc' => $input['desc'],
                'picture' => $input['picture'],
                'ord' => $input['ord'],
                'video' => $input['video'],
                'price' => $input['price'],
                'content' => $input['content'],
                'status' => $input['status'],
                'cate_id' => $input['cate_id'],
                'type' => $input['type'],
                'rate' => $input['rate'],
            );
            if ($input['id']) {
                $data['id'] = $input['id'];
                $video = \app\common\model\Video::getOne(['id'=>$data['id']]);
            }
            if ($data['id']) {
                if($video['picture']!=$input['picture']){
                    model('qiniu')->remove($video['picture']);//删除图集
                }
                if($video['video']!=$input['video']){
                    model('qiniu')->remove($video['video']);//删除视频
                }
                $res = $this->videoModel->isUpdate(true)->save($data);
            } else {
                $res = $this->videoModel->isUpdate(false)->save($data);
            }
            if ($res) {
                return returnJson(0, '操作成功');
            }
            return returnJson(1, '网络异常，请重试');
        }
    }

    /**
     * 课程删除
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function del_video()
    {
        if (request()->isPost()) {
            $data = input('');
            if ($data['id']) {
                $video = $this->videoModel->where('id', $data['id'])->find();
                model('qiniu')->remove($video['picture']);//删除图集
                model('qiniu')->remove($video['video']);//删除视频

                if ($video) {
                    $res = $this->videoModel->where('id', $data['id'])->delete();
                    if ($res) {
                        return returnJson(200, '删除成功');
                    } else {
                        return returnJson(202, '删除失败');
                    }
                }
            }
        }
        return returnJson(202, '分类不存在');
    }

    /**
     * 开播设置
     * @return \think\response\Json
     */
    public function setLive()
    {
        $input = input();
        Config::saveConfig([
            'aliyun_live_open' => $input['aliyun_live_open'],
            'live_manager' => $input['live_manager']
        ]);
        if ($input['aliyun_live_open'] == 1) {
            //$info = Live::getLiveInfo();
            return returnJson(0, '设置成功');
        }
        return returnJson(0, '设置成功', false);
    }

    /**
     * 课程分类列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function video_cate()
    {
        if (request()->isPost()) {
            $data = input('');
            $list = $this->videoCateModel->order('ord desc')->select();
            return returnJson(200, '', $list);
        }
    }

    /**
     * 课程分类编辑/添加
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit_cate()
    {
        if (request()->isPost()) {
            $data = input('');
            if ($data['id']) {
                $cate = $this->videoCateModel->where('id', $data['id'])->find();
                if (!$cate) {
                    return returnJson(202, '分类不存在');
                }
            }
            if ($data['name']) {
                $cate = $this->videoCateModel->where('name', $data['name'])->find();
                if ($cate) {
                    $cate = $cate->toArray();
                    if ($cate['id'] != $data['id']) {
                        return returnJson(202, '该分类已存在');
                    }
                }
            } else {
                return returnJson(202, '请填入分类名称');
            }
            $save_data = [
                'name' => $data['name'],
                'ord' => $data['ord'],
                'update_time' => time(),
            ];
            if ($data['id']) {
                $res = $this->videoCateModel->save($save_data, ['id' => $data['id']]);
                if ($res) {
                    return returnJson(200, '修改成功', $data['id']);
                } else {
                    return returnJson(202, '修改失败');
                }
            } else {
                $save_data['create_time'] = time();
                $res = $this->videoCateModel->insertGetId($save_data);
                if ($res) {
                    return returnJson(200, '添加成功', $res);
                } else {
                    return returnJson(202, '添加失败');
                }
            }

        }
    }

    /**
     * 课程分类删除
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function del_cate()
    {
        if (request()->isPost()) {
            $data = input('');
            if ($data['id']) {
                $cate = $this->videoCateModel->where('id', $data['id'])->find();
                if ($cate) {
                    $res = $this->videoCateModel->where('id', $data['id'])->delete();
                    if ($res) {
                        return returnJson(200, '删除成功');
                    } else {
                        return returnJson(202, '删除失败');
                    }
                }
            }
        }
        return returnJson(202, '分类不存在');
    }

    /**
     * 课程订阅
     * @return \think\response\Json
     */
    public function video_pay()
    {
        if (request()->isPost()) {
            $data = input('');
            $where = [];
            $page = max($data['page'], 1);
            $size = max($data['size'], 20);
            if ($data['uid']) {
                $where['a.uid'] = $data['uid'];
            }
            if ($data['title']) {
                $where['a.title'] = [
                    'like', "%{$data['title']}%"
                ];
            }
            $list = VideoPayService::S()->queryPage($where, $page, $size, 'a.*,b.title,b.type');
            $total = VideoPayService::S()->queryCount($where);
            return returnJson(200, '', [
                'list' => $list,
                'page' => $page,
                'size' => $size,
                'total' => $total
            ]);
        }
    }

}
